﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using OutLook.Properties.Calc.Structures;


namespace OutLook.Properties.Calc
{
    public static class ElementsData
    {
        #region Methods

        /// <summary>
        /// Getting element by its name
        /// </summary>
        /// <param name="ElementName">Element name</param>
        /// <returns>All Element Data</returns>
        public static Element GetElement(string ElementName)
        {
            return Elements.Select(el => el).Where(el => el.Name.Equals(ElementName)).First();
        }

        /// <summary>
        /// Getting oxide by its string discribe
        /// </summary>
        /// <param name="ElementName">String Describe of oxide</param>
        /// <returns>Oxide</returns>
        public static Oxide GetOxide(string ElementName)
        {
            Oxide oxide = new Oxide();

            Regex regEx = new Regex("([A-Z]{1}[a-z]*)(\\d*)");
            MatchCollection matchCollection = regEx.Matches(ElementName);

            int elementNumber = 1;
            if (matchCollection.Count > 0)
            {
                foreach(Match match in matchCollection)
                {
                    GroupCollection groupCollection = match.Groups;
                    
                    int coef;
                    try
                    {
                        coef = Convert.ToInt32(groupCollection[2].Value);
                    }
                    catch (Exception)
                    {
                        coef = 1;
                    }


                    if (elementNumber == 1)
                    {
                        oxide.CationName = groupCollection[1].Value;
                        oxide.CationFeature = GetElement(groupCollection[1].Value);
                        oxide.CationCoefficient = coef;
                    }
                    else
                    {
                        oxide.AnionName = groupCollection[1].Value;
                        oxide.AnionFeature = GetElement(groupCollection[1].Value);
                        oxide.AnionCoefficient = coef;
                    }
                    
                    elementNumber++;
                }
            }
            return oxide;
        }

        #endregion

        #region Private array

        private static readonly Element[] Elements = 
       { Element.Create("H",	1.01,	0.529,	0.156,	0.6407,	1.187),
Element.Create("Li",	6.94,	1.57,	0.36,	0.2777,	3.523),
Element.Create("Be",	9.01,	1.13,	0.265,	0.3772,	2.5357),
Element.Create("B",	10.81,	0.92,	0.198,	0.5049,	2.042),
Element.Create("C",	12.01,	0.51,	0.118,	0.8472,	1.1444),
Element.Create("N",	14.01,	0.61,	0.13,	0.769,	1.3688),
Element.Create("O",	16,	0.73,	0.136,	0.7351,	1.6381),
Element.Create("F",	18.99,	0.96,	0.142,	0.704,	2.1542),
Element.Create("Ne",	20.18,	1.6,	0.16,	0.6247,	3.5903),
Element.Create("Na",	22.99,	1.91,	0.285,	0.3508,	4.286),
Element.Create("Mg",	24.31,	1.6,	0.196,	0.5101,	3.5903),
Element.Create("Al",	26.98,	1.468,	0.156,	0.6408,	3.2941),
Element.Create("Si",	28.09,	0.93,	0.091,	1.0986,	2.0869),
Element.Create("P",	30.97,	1.04,	0.095,	1.0523,	2.3337),
Element.Create("S",	32.06,	1.16,	0.1,	0.9997,	2.603),
Element.Create("Cl",	35.45,	1.41,	0.1055,	0.9476,	3.164),
Element.Create("Ar",	39.95,	1.94,	0.115,	0.8691,	4.3532),
Element.Create("K",	39.1,	2.37,	0.26,	0.3845,	5.3182),
Element.Create("Ca",	40.08,	2.02,	0.151,	0.662,	4.5328),
Element.Create("Sc",	44.96,	1.61,	0.101,	0.9898,	3.6128),
Element.Create("Ti",	47.9,	1.45,	0.085,	1.1761,	3.2537),
Element.Create("V",	50.94,	1.348,	0.075,	1.3329,	3.0249),
Element.Create("Cr",	51.99,	1.27,	0.066,	1.5147,	2.8723),
Element.Create("Mn",	54.94,	1.31,	0.082,	1.2191,	2.9396),
Element.Create("Fe",	55.85,	1.26,	0.088,	1.136,	2.8274),
Element.Create("Co",	58.93,	1.28,	0.097,	1.0306,	2.8723),
Element.Create("Ni",	58.7,	1.275,	0.108,	0.9256,	2.8611),
Element.Create("Cu",	63.54,	1.305,	0.118,	0.8472,	2.9284),
Element.Create("Zn",	65.38,	1.37,	0.135,	0.7405,	3.0742),
Element.Create("Ga",	69.72,	1.65,	0.147,	0.6801,	3.7025),
Element.Create("Ge",	72.59,	1.09,	0.08,	1.2496,	2.4459),
Element.Create("As",	74.92,	1.22,	0.0835,	1.1972,	2.7376),
Element.Create("Se",	78.96,	1.35,	0.088,	1.136,	3.0294),
Element.Create("Br",	79.9,	1.61,	0.0935,	1.0692,	3.6128),
Element.Create("Kr",	83.8,	2.03,	0.095,	1.0521,	4.5551),
Element.Create("Rb",	85.47,	2.52,	0.225,	0.4443,	5.6548),
Element.Create("Sr",	87.62,	2.2,	0.143,	0.6991,	4.9267),
Element.Create("Y",	88.91,	1.81,	0.09,	1.1108,	4.0616),
Element.Create("Zr",	91.22,	1.6,	0.074,	1.3509,	3.5903),
Element.Create("Nb",	92.91,	1.465,	0.063,	1.5868,	3.2874),
Element.Create("Mo",	95.94,	1.395,	0.055,	1.8176, 3.1303),
Element.Create("Tc",	98.91,	1.36,	0.07,	1.4279,	3.0517),
Element.Create("Ru",	101.07,	1.34,	0.075,	1.3329,	3.0069),
Element.Create("Rh",	102.91, 1.385,	0.085,	1.1761,	3.1079),
Element.Create("Pd",	106.4,	1.41,	0.096,	1.0414,	3.164),
Element.Create("Ag",	107.87,	1.48,	0.086,	1.1624,	3.3211),
Element.Create("Cd",	112.41,	1.51,	0.097,	1.0306,	3.3884),
Element.Create("In",	114.82,	1.66,	0.106,	0.9431,	3.725),
Element.Create("Sn",	118.69,	1.35,	0.0725,	1.3789,	3.0294),
Element.Create("Sb",	121.75,	1.45,	0.074,	1.3509,	3.2537),
Element.Create("Te",	127.6,	1.57,	0.076,	1.3154,	3.523),
Element.Create("I",	126.9,	1.815,	0.078,	1.2817,	4.0728),
Element.Create("Xe",	131.29,	2.18,	0.08,	1.2494,	4.8917),
Element.Create("Cs",	132.91,	2.68,	0.2,	0.4998,	6.0587),
Element.Create("Ba",	137.33,	2.3,	0.115,	0.8693,	5.0265),
Element.Create("La",	138.91,	1.87,	0.073,	1.3695,	4.1962),
Element.Create("Lu",	174.97,	1.6,	0.078,	1.2817,	3.5903),
Element.Create("Hf",	178.49,	1.59,	0.07,	1.4281,	3.5679),
Element.Create("Ta",	180.95,	1.469,	0.06,	1.6662,	3.2964),
Element.Create("W",	183.85,	1.42,	0.053,	1.8862,	3.1864),
Element.Create("Re",	186.21,	1.34,	0.073,	1.3694,	3.0069),
Element.Create("Os",	190.2,	1.35,	0.076,	1.3154,	3.0294),
Element.Create("Ir",	192.22,	1.39,	0.084,	1.1901,	3.1191),
Element.Create("Pt",	195.09,	1.42,	0.098,	1.0201,	3.1864),
Element.Create("Au",	196.97,	1.48,	0.054,	1.8513,	3.3143),
Element.Create("Hg",	200.59,	1.55,	0.068,	1.4701,	3.4781),
Element.Create("Tl",	204.37,	1.71,	0.085,	1.1761,	3.8372),
Element.Create("Pb",	207.2,	1.53,	0.0675,	1.481,	3.4333),
Element.Create("Bi",	208.98,	1.63,	0.068,	1.4701,	3.6577),
Element.Create("Po",	208.98,	1.72,	0.0685,	1.4591,	3.8595),
Element.Create("At",	209.99,	1.95,	0.069,	1.4486,	4.3756),
Element.Create("Rn", 222.02, 2.3, 0.0695, 1.4381, 5.161),
        };

        #endregion

    }
}
